﻿define([
    'domReady!',
    'lyear-loading',
    'jquery.ajaxRequest'
], function (domReady) {

    function init() {
        //页面数据绑定
        dataBind();
    }

    /**
     * 页面数据绑定
     */
    function dataBind() {
        $('.page-loading').ajaxRequest("/BMS/Role/GetRoleFunctionList", { id: id }, function (data) {
            var html = "";
           
            $(".table tbody").html("");
       
            $.each(data.data, function (i, value) {
                var level = 0;
                var pl = 20;

                html += "<tr>" +
                    "<td>" +
                    '<div class="custom-control custom-checkbox">' +
                    ' <input type="checkbox" name="functionId" class="custom-control-input checkbox-parent" id="roleid-' + value.roleId + '" dataid="id-' + value.roleId + '" value="' + value.id + '" '; if (value.isChecked) { html += 'checked=true';} html +='>'+
                                   ' <label class="custom-control-label" for="roleid-'+ value.roleId + '">' + value.name + '</label>'+
                               '</div>'+
                           '</td>'+
                        '</tr>';
               
                html = renderData(value, html, level, pl);
            });
      
            $(".table tbody").append(html);
            checkEventBind()
        });
    }

    function renderData(data, html, level, pl) {
        if (!data.child) {
            return html;
        }
        level++;
        var tmpHtml = `<tr><td style="padding-left: ` + (level * pl) + `px!important;">`;
        $.each(data.child, function (i, value) {
            switch (value.type) {
                case 0: html += `<tr>
                        <td style="padding-left:`+ (level * pl) + `px!important;">
                            <div class="custom-control custom-checkbox">
                                <input type="checkbox" name="functionId" class="custom-control-input checkbox-parent checkbox-child" id="roleid-`+ value.roleId + `" dataid="id-` + value.roleId + `" value="` + value.id + `" `; if (value.isChecked) { html += `checked=true`;} html +=`>
                                <label class="custom-control-label" for="roleid-`+ value.roleId + `">` + value.name + `</label>
                            </div>
                        </td>
                    </tr>`;
                    html = renderData(value, html, level, pl);
                    break;
                case 1: tmpHtml += `<div class="custom-control custom-checkbox custom-control-inline">
                                    <input type="checkbox" name="functionId" class="custom-control-input checkbox-child" id="roleid-`+ value.roleId + `" dataid="id-` + value.roleId + `" value="` + value.id + `" `; if (value.isChecked) { tmpHtml += `checked=true`; } tmpHtml +=`>
                                    <label class="custom-control-label" for="roleid-`+ value.roleId + `">` + value.name + `</label>
                                </div>`;
                    if (i == data.child.length - 1) {
                        tmpHtml += `</td></tr>`;
                        html += tmpHtml;
                        return html;
                    }
                    break;
                default:
                    return html;
                    break;
            }
        })
        return html;
    }

    /**
     * 事件绑定
     * */
    function checkEventBind() {
        // 复选框全选
        $("#check-all").change(function () {
            if ($boxname = $(this).data('name')) {
                $(this).closest('table').find("input[name='" + $boxname + "']").prop('checked', $(this).prop("checked"));
            } else {
                $(this).closest('table').find(".custom-checkbox input[type='checkbox']").prop('checked', $(this).prop("checked"));
            }
        });
        //动态选择框，上下级选中状态变化
        $('input.checkbox-parent').on('change', function () {
            var dataid = $(this).attr("dataid");
            $('input[dataid^=' + dataid + '-]').prop('checked', $(this).is(':checked'));
        });
        $('input.checkbox-child').on('change', function () {
            var dataid = $(this).attr("dataid");
            dataid = dataid.substring(0, dataid.lastIndexOf("-"));
            var parent = $('input[dataid=' + dataid + ']');
            if ($(this).is(':checked')) {
                parent.prop('checked', true);
                //循环到顶级
                while (dataid.lastIndexOf("-") != 2) {
                    dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                    parent = $('input[dataid=' + dataid + ']');
                    parent.prop('checked', true);
                }
            } else {
                //父级
                if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
                    parent.prop('checked', false);
                    //循环到顶级
                    while (dataid.lastIndexOf("-") != 2) {
                        dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                        parent = $('input[dataid=' + dataid + ']');
                        if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
                            parent.prop('checked', false);
                        }
                    }
                }
            }
        });
    }

    function eventBind() {
        $(document).on('click', '#btnSave', function () {
            var functionIdArr = [];
            var checkbox = $("input[name='functionId']:checked");
            if (checkbox.length) {
                $.each(checkbox, function (i, value) {
                    functionIdArr.push($(this).val())
                })
            }
           
            $(this).ajaxRequest("/BMS/Role/SaveRoleFunction", { roleId: id, functionIdArr: functionIdArr })
        });
    }

    $(document).ready(function () {
        init();
        eventBind();
    })

});